<?php

/**
 * Controller for files
 * Extends the MasterController
 */

class FileController extends MasterController {

    protected $loginModel ;

    /**
     * Extends the MasterControllers construct so we can add the loginModel
     *
     * @param object $fv FileView
     * @param object $fm FileModel
     * @param object $lm LoginModel (so we can check if user is logged in)
     * 
     * @returns void
     *
     * TODO: Make abstract base-classes for views, models and controllers. Then I can type the objects.
     */

    public function __construct($fv, $fm, $lm) {
        parent::__construct($fv, $fm) ;
        $this->loginModel = $lm ;
    }

    /**
     * Run a check to see if user is logged in or not, then render the views depending.
     * @return string $retStr
     *
     * TODO: Some error-handling. Perhaps an Error-class?
     */
    public function doControll() {

        $retStr = "" ;

        // check if file is uploaded
        if($this->view->getUploadedFile()) {
            $this->model->saveFile($this->view->getUploadedFile()) ;
        }

        if($this->loginModel->isLoggedIn()) {
            $retStr .= $this->view->showFileUploadForm() ;
        }

        $retStr .= $this->view->showFileList($this->model->getFiles(), "Public/Files/") ;

        return $retStr ;
    
    }

}
